package com.grasscove.namethat.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.grasscove.namethat.R;

public class WordsListAdapter extends SimpleCursorAdapter {

    Cursor mCursor;
    Context mContext;

    public WordsListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        mCursor = c;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mCursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        mCursor.moveToPosition(position);

        return mCursor.getString(mCursor.getColumnIndex(WordsDbAdapter.KEY_WORD));
    }

    @Override
    public long getItemId(int position) {
    	mCursor.moveToPosition(position);

        return mCursor.getInt(mCursor.getColumnIndex(WordsDbAdapter.KEY_ID));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout row;

        if (convertView == null) {
            row = new RelativeLayout(mContext);

            LayoutInflater vi;
            vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi.inflate(R.layout.words_row, row, true);
        } else {
            row = (RelativeLayout) convertView;
        }

        mCursor.moveToPosition(position);
        String word = mCursor.getString(mCursor.getColumnIndex(WordsDbAdapter.KEY_WORD));

        TextView wordTextView = (TextView) row.findViewById(R.id.words_row_text);
        wordTextView.setText(word);

        return row;
    }
}